package com.tocici.controller;

import java.util.Date;
import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import com.tocici.domain.vo.UserUseTimeVo;
import com.tocici.domain.bo.UserUseTimeBo;
import com.tocici.service.IUserUseTimeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 用户听歌时段
 *
 * @author zeraul
 * @date 2025-03-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/music/useTime")
public class UserUseTimeController extends BaseController {

    private final IUserUseTimeService userUseTimeService;

    /**
     * 查询用户听歌时段列表
     */
    @SaCheckPermission("music:useTime:list")
    @GetMapping("/list")
    public TableDataInfo<UserUseTimeVo> list(UserUseTimeBo bo, PageQuery pageQuery) {
        return userUseTimeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户听歌时段列表
     */
    @SaCheckPermission("music:useTime:export")
    @Log(title = "用户听歌时段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserUseTimeBo bo, HttpServletResponse response) {
        List<UserUseTimeVo> list = userUseTimeService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户听歌时段", UserUseTimeVo.class, response);
    }

    /**
     * 获取用户听歌时段详细信息
     *
     * @param timeId 主键
     */
    @SaCheckPermission("music:useTime:query")
    @GetMapping("/{timeId}")
    public R<UserUseTimeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long timeId) {
        return R.ok(userUseTimeService.queryById(timeId));
    }

    /**
     * 新增用户听歌时段
     */
    @SaCheckPermission("music:useTime:add")
    @Log(title = "用户听歌时段", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserUseTimeBo bo){
        return toAjax(userUseTimeService.insertByBo(bo));
    }

    /**
     * 修改用户听歌时段
     */
    @SaCheckPermission("music:useTime:edit")
    @Log(title = "用户听歌时段", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserUseTimeBo bo){
        return toAjax(userUseTimeService.updateByBo(bo));
    }

    /**
     * 删除用户听歌时段
     *
     * @param timeIds 主键串
     */
    @SaCheckPermission("music:useTime:remove")
    @Log(title = "用户听歌时段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{timeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] timeIds) {
        return toAjax(userUseTimeService.deleteWithValidByIds(List.of(timeIds), true));
    }

    @PostMapping("/usedTime")
    @SaIgnore
    public R<List<Integer>> getUsedTimeList(@RequestParam("start") @DateTimeFormat(pattern = "yyyy-MM-dd") Date start, @RequestParam("end") @DateTimeFormat(pattern = "yyyy-MM-dd") Date end){
        List<Integer> usedTime = userUseTimeService.getUsedTime(start, end);
        System.out.println("usedTime = " + usedTime);
        return R.ok(usedTime);
    }
}
